<template>
  <div>
    <el-dialog title="编辑笔记本标题简介" :visible.sync="dialogFormVisible"  :before-close="handleClose" >
      <el-form :model="form">
        <el-form-item label="笔记名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="笔记简介" :label-width="formLabelWidth">
          <el-input type="textarea" :rows="3" v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible=false">取消</el-button>
        <el-button type="primary" @click="updateNote">确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
    export default {
      name: "NoteEditForm",
      data() {
        return {
          isCreate:false,
          dialogFormVisible: false,
          formLabelWidth: '120px',
          form: {
            name:"",
            description:""
          },
          cid:'',
          baseURL: this.$baseURL.note,
        }
      },

      methods: {
        handleClose(done) {
          var _this = this
          this.$confirm('修改并关闭？')
            .then(_ => {
              _this.updateNote()
              done();
            })
            .catch(_ => {});
        },
        updateNote(){
          var _this = this
          var url
          var requestData = {
            name: this.form.name,
            description: this.form.description  // 映射字段名
          }
          //isCreate表示是否要创建
          if(!_this.isCreate) {
            console.log(this.cid)
            url = `${this.baseURL}/update/${this.form.id}`
            requestData.id = this.form.id
          }
          else{
            url = `${this.baseURL}/create/${this.cid}`
          }
          this.axios.post(url,requestData)
            .then(function (response) {
              if(response.data.code === 0){
                _this.dialogFormVisible = false
                _this.$message({
                  message: '更新成功',
                  type: 'success'
                });
                _this.$emit('updateInfo')
              }
              else {
                this.$message.error('更新失败');
              }
            })
            .catch(function (error) {
              this.$message.error(error);
            })
        },
      }
    }
</script>

<style scoped>

</style>
